//
//  VideoDetaileViewController.m
//  ToYourHeartNews
//
//  Created by XJ－Mac on 16/2/10.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "VideoDetaileViewController.h"
#import "BlackManager.h"
#import "SetBlack.h"
#import "DateBaseManager.h"
#import "Videomodel.h"
#import "LoginViewController.h"
#import "UMSocial.h"//友盟分享
@interface VideoDetaileViewController ()
@property (nonatomic, retain)UIButton *likeBtn;
@property (nonatomic, assign)NSInteger mId;
@property (nonatomic, assign)BOOL collect;
@property (nonatomic, assign)NSMutableArray *arr;
@end

@implementation VideoDetaileViewController
-(void)dealloc
{
    [_pathhtml release];
    [_pathstr release];
    [_model release];
    [_likeBtn release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-newlisticon06"] style:UIBarButtonItemStylePlain target:self action:@selector(BackAction)] autorelease];
    //shareBtn
    UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-share"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _likeBtn.frame = CGRectMake(0, 0, 30, 30);
    //根据数据库中是否有该model设置收藏按钮样式
    DateBaseManager *dbManager = [DateBaseManager shareInstance];
    self.arr = [dbManager select];
    self.i = NO;

    
        for (Videomodel *m in _arr) {
            if ([_model.title isEqualToString:m.title]) {
                _i = YES;
            }
        }
    
    
    if (_i) {
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
    }else{
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
    }
    
    [_likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    //用按钮创建BarButton
    UIBarButtonItem *collectBar = [[UIBarButtonItem alloc]initWithCustomView:_likeBtn];
    self.navigationItem.rightBarButtonItems = @[collectBar, shareBtn];
    
    //webView
    UIWebView *webVC = [[UIWebView alloc]initWithFrame:CGRectMake(0, -75, self.view.frame.size.width, self.view.frame.size.height + 75)];
   
    webVC.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:webVC];
    if ([BlackManager sharedInstance].isNight) {
        [webVC stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.background=‘#F6F7F3‘"];
    }
    //webView适应
    webVC.scalesPageToFit = YES;
    if (_pathstr.length != 0) {
        [webVC loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_pathstr]]];
    }else{
        
        [webVC loadHTMLString:_pathhtml baseURL:[NSURL URLWithString:_pathhtml]];
    }
    [webVC release];
}
#pragma mark - 分享方法
-(void)shareAction
{
    NSLog(@"分享了");
    [UMSocialData setAppKey:@"56a877ab67e58e508f000fb1"];
    [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeVideo url:_model.mp4_url];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:Nil shareText:_model.title shareImage:nil shareToSnsNames:@[UMShareToSina, UMShareToTencent,UMShareToRenren] delegate:nil];
}

#pragma mark - 收藏方法
-(void)likeAction:(UIButton *)btn
{
    if (_i) {
        //设置收藏属性
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
        _arr = [[DateBaseManager shareInstance] select];
        for (Videomodel *m in _arr) {
            if ([m.title isEqualToString:_model.title]) {
                [[DateBaseManager shareInstance] deleteVideoWithId:m.videoId];
            }
        }
        
        NSLog(@"取消了收藏");
        _i = !_i;

    }else{
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
         [[DateBaseManager shareInstance] insertVideo:_model];
        [[DateBaseManager shareInstance] select];
        _i = !_i;
    }
}

    

-(void)BackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
